﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;

namespace jsGotoDefinition.VS2010.Helpers
{
    /// <summary>
    /// An object representing a list of FindRegEx objects used for searching for the definition of a jaavascript method/object/function/variable
    /// <seealso cref="FindRegEx"/>
    /// </summary>
    public class FindRegExCollection : List<FindRegEx>
    {
        public enum RegExMode
	    {
	         FindAllReferences,
             FindDefinition
	    }

        private string searchname = "";

        /// <summary>
        /// Constructor that builds the list from settings in an xml document
        /// </summary>
        /// <param name="xmldoc"></param>
        public FindRegExCollection(XmlDocument xmldoc, RegExMode mode) {
            string regexnodespath = string.Empty;
            switch (mode) {
                case RegExMode.FindAllReferences:
                    regexnodespath = "//settings/allReferencesregularexpressions/regex";
                    break;
                case RegExMode.FindDefinition:
                    regexnodespath = "//settings/regularexpressions/regex";
                    break;
                default:
                    regexnodespath = "//settings/regularexpressions/regex";
                    break;
            }

            XmlNodeList xmlnodes = xmldoc.SelectNodes(regexnodespath);
            foreach (XmlNode node in xmlnodes) {
                this.Add(new FindRegEx() { RegExTemplate = node.InnerText });
            }

        }


        /// <summary>
        /// The name of the function/method/variable whose definition is being searched for
        /// </summary>
        public string SearchName {
            get { return searchname; }
            set {
                foreach (FindRegEx item in this) {
                    item.SearchName = value;
                };
            }
        }
    }
}
